package com.ssdp.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.ssdp.constant.SMSConstant;
import lombok.extern.slf4j.Slf4j;

/**
 * @author syo 2319615164@qq.com
 * @description 短信发送工具类
 * @date 2024/3/14
 * @Copyright syo
 */
@Slf4j
public class SMSUtils {

    /**
     * 发送短信
     * @param signName 签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param 参数(验证码)
     */
    public static void sendMessage(String signName, String templateCode, String phoneNumbers, String param){
        DefaultProfile profile = DefaultProfile.getProfile(
                    SMSConstant.REGION_ID,
                    SMSConstant.ACCESS_KEY_ID,
                    SMSConstant.SECRET);

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId(SMSConstant.REGION_ID);
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);

        request.setTemplateParam("{\"code\":\""+param+"\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            log.info("短信发送成功");
        }catch (ClientException e) {
            log.error("短信发送失败", e);
        }
    }

    /**
     * 测试邮件发送功能
     * @param args 参数
     */
    public static void main(String[] args) {
        SMSUtils.sendMessage(
                SMSConstant.SIGN_NAME,
                SMSConstant.TEMPLATE_CODE, "17313899542", "1234");
    }
}
